# -*- coding:utf-8 -*-
import sys
import time
from multiprocessing import Process, Pool
from test.test_suite.suite_yaml import app_test
from utils.report import send_report
from utils.server import start_server
from utils import get_config as GC, common
sys.path.append(GC.configs['project_path'])
# 实际连接的设备
attached_devices = common.get_online_devices()

data = {"init": [], "info": []}


def start_process():
    """
    进程启动
    :return:
    """
    desired_process = []
    appium_process = []

    for device in attached_devices:
        print(device)
        desired = Process(target=app_test, args=(device,))
        desired_process.append(desired)
        appium = Process(target=start_server, args=(device,))
        appium_process.append(appium)
    # 并发启动appium服务
    for appium in appium_process:
        appium.start()
    for appium in appium_process:
        appium.join()
    time.sleep(5)
    # 启动多设备执行测试
    for desired in desired_process:
        desired.start()
    for desired in desired_process:
        desired.join()


def start_single():
    """
    单线程启动
    :return:
    """
    for device in attached_devices:

        start_server(device)
        time.sleep(5)
        app_test(serial=device)


if __name__ == '__main__':
    if len(attached_devices) > 0:
        start_test_time = time.strftime('%Y-%m-%d %H:%M %p')
        start_time = time.time()
        start_process()
        end_time = time.time()
        # 获取相关信息，写入 Excel
        send_report(data, start_test_time, start_time, end_time)
    else:
        print(u"设备不存在")
